<?php
/**
 * Created by PhpStorm.
 * User: 17208
 * Date: 2019/6/13
 * Time: 15:42
 */

namespace app\server;
use app\api\model\Team_user;
use app\server\model\Team_team;
use app\server\model\User;
use think\Exception;
use app\common\facade\WebSocketToken;
use app\server\model\Chat_record;
use app\common\lib\Export;

class service
{

    private $cmd_data = [
        "100"=>"chat_send_single",   //发送个人消息
        "101"=>"chat_send_team",   //发送群体消息
    ];
    private $fd;
    private $user_id;
    public function __construct($fd){
        $user_id = WebSocketToken::get_user_id($fd);
        $this->user_id = $user_id;
        $this->fd = $fd;
    }

    public function run($type,$data){
        try{
            $cmd_data = $this->cmd_data;
            if(!isset($cmd_data[$type])) throw new Exception("type不存在");

            $method = $cmd_data[$type];
            $this->$method($data);
        }catch (Exception $e){
            Export::socket_exp_err($this->fd,$e->getMessage());
        }
    }
    /** 100 发送个人消息 */
    private function chat_send_single($data){
        $data = $this->check_fields($data,['user_id',"message"]);

        $user_id = $data['user_id'];
        $user = User::get($user_id);
        if(!$user) throw new Exception("用户不存在");

        // 对方存在则发送
        $send_fd = WebSocketToken::get_fd($user_id);
        if(WebSocketToken::exist($send_fd)){
            $data = [
                "message"=>$data['message'],
                "user"=>[
                   "id"=>$user_id,
                   "name"=>$user->name,
                   "pic"=>$user->pic_url,
                ],
            ];
            Export::socket_exp_ok($send_fd,Export::$socket_single_chat_cmd,["data"=>$data]);
            $is_receive = 1;
        }else{
            $is_receive = 0;
        }

        Chat_record::create([
            "user_id"=>$user_id,
            "send_id"=>$this->user_id,
            "send_content"=>$data['message'],
            "is_receive"=>$is_receive
        ]);

        Export::socket_exp_ok($this->fd);
    }
    /** 101 发送消息至团队 */
    public function chat_send_team($data){
        $data = $this->check_fields($data,['team_id',"message"]);

        $team = Team_team::get($data['team_id']);
        if(!$team) throw new Exception("聊天组不存在");

        $team_user = Team_user::where(["team_id"=>$data['team_id']])->select()->toArray();
        $user_ids = array_column($team_user,"user_id");
        if(!in_array($this->user_id,$user_ids)) throw new Exception("您不在该聊天组！");

        $record_list = [];
        foreach ($team_user as $user){
            // 检验用户是否在线
            $fd = WebSocketToken::get_fd($user['id']);
            $is_exist = WebSocketToken::exist($fd);
            if($is_exist){
                $data = [
                    "message"=>$data['message'],
                    "user"=>[
                        "id"=>$user['id'],
                        "name"=>$user['name'],
                        "pic"=>$user['pic_url'],
                    ],
                ];
                Export::socket_exp_ok($fd,Export::$socket_team_chat_cmd,["data"=>$data]);
                $is_receive = 1;
            }else{
                $is_receive = 0;
            }
            $record_list[] = [
                "user_id"=>$user['id'],
                "send_id"=>$this->user_id,
                "team_id"=>$data['team_id'],
                "send_content"=>$data['message'],
                "is_receive"=>$is_receive,
            ];
        }
        // 存入数据库
        $chat_record = new Chat_record();
        $chat_record->saveAll($record_list);

    }



    /** -----------------------------------内部方法------------------------------ */

    /** 字段校验 */
    public function check_fields($data,$fields){
        if(!is_array($data)) throw new Exception("参数错误");
        foreach ($fields as $item){
            if(!isset($data[$item])) throw new Exception("参数错误");
        }
        return $data;
    }

}